package br.uvv.sao.managedBean;

import java.io.Serializable;

import javax.annotation.PostConstruct;

import br.uvv.sao.domain.Aluno;
import br.uvv.sao.domain.Professor;
import br.uvv.sao.domain.Usuario;
import br.uvv.sao.rn.LoginRN;
import br.uvv.sao.util.MsgUtil;

public class LoginBean implements Serializable {
	private static final long serialVersionUID = -5836669670263268220L;
	
	private Usuario usuarioInstance;
	private Aluno alunoInstance;
	private Professor professorInstance;
	private LoginRN loginRN;
	
	public String goRecuperarSenha(){
		usuarioInstance = new Usuario();
		alunoInstance = new Aluno();
		professorInstance = new Professor();
		return "goRecuperarSenha";
	}
	
	public String login(){
		System.out.println("AQUI!");
		if (loginRN == null)
			loginRN = new LoginRN();
		Usuario usuarioAutenticado = loginRN.login(usuarioInstance);
		if(usuarioAutenticado != null){
			if(usuarioAutenticado instanceof Aluno){
				alunoInstance = loginRN.carregarAluno(usuarioAutenticado);
				usuarioInstance = alunoInstance;
				professorInstance = new Professor();
			}else{
				alunoInstance = new Aluno();
				professorInstance = loginRN.carregarProfessor(usuarioAutenticado);
				usuarioInstance = professorInstance;
			}
			MsgUtil.msgInfo("Bem Vindo, " + usuarioAutenticado.getNomeCompleto());
		}else{
			MsgUtil.msgError("ERRO: Usuário ou Senha inválidos.");
		}
		return "goHome";
	}
	
	public String logout(){
		usuarioInstance = new Usuario();
		alunoInstance = new Aluno();
		professorInstance = new Professor();
		return "goHome";
	}
	
	public String recuperarSenha(){
		String retorno = loginRN.recuperarSenha(usuarioInstance);
		if(retorno.contains("ERRO:")){
			MsgUtil.msgError(retorno);
			return null;
		}else{
			MsgUtil.msgInfo(retorno);
		}
		return null;
	}
	
	@PostConstruct
	public void init(){
		usuarioInstance = new Usuario();
		alunoInstance = new Aluno();
		professorInstance = new Professor();
		loginRN = new LoginRN();
	}
	
	public Usuario getUsuarioInstance() {
		return usuarioInstance;
	}
	public void setUsuarioInstance(Usuario usuarioInstance) {
		this.usuarioInstance = usuarioInstance;
	}
	public Aluno getAlunoInstance() {
		return alunoInstance;
	}
	public void setAlunoInstance(Aluno alunoInstance) {
		this.alunoInstance = alunoInstance;
	}
	public Professor getProfessorInstance() {
		return professorInstance;
	}
	public void setProfessorInstance(Professor professorInstance) {
		this.professorInstance = professorInstance;
	}
	public LoginRN getLoginRN() {
		return loginRN;
	}
	public void setLoginRN(LoginRN loginRN) {
		this.loginRN = loginRN;
	}
}
